package colicious.service;

import org.springframework.validation.Validator;
import org.springframework.validation.Errors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LoginValidator implements Validator {
	
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	public boolean supports(Class clazz) {
		return Login.class.equals(clazz);
	}
	
	public void validate(Object obj, Errors errors) {
		Login log = (Login) obj;
		if (log == null) {
			errors.rejectValue("login", "error.not-specified", null, "Value required.");
		}
		else {
			logger.info("Validating with " + log);
			if ( !log.getLogin().equals("test") ) {
				errors.rejectValue("login", "error.badli", new Object[] {new String(log.getLogin())}, "Value too high.");
			}
			if ( !log.getPassword().equals("test") ) {
				errors.rejectValue("password", "error.badpw", new Object[] {new String(log.getPassword())}, "Value too low.");
			}
		}
	}
}